package com.i8office.android.zhihuixinwu.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cyan.tools.base.BaseViewHolder;
import com.cyan.tools.base.RecyclerArrayAdapter;
import com.i8office.android.zhihuixinwu.R;
import com.i8office.android.zhihuixinwu.bean.People;
import com.i8office.android.zhihuixinwu.bean.Send;

import java.util.ArrayList;
import java.util.List;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by pc on 2017/8/18.
 */

public class PeopleAdapter extends RecyclerArrayAdapter<People> {
    OnDelectPeople onDelectPeople;
    Send send;

    public PeopleAdapter(Context context, Send send, OnDelectPeople onDelectPeople) {
        super(context);
        this.onDelectPeople = onDelectPeople;
        this.send = send;
    }

    public PeopleAdapter(Context context) {
        super(context);
    }

    private boolean is_single;
    private boolean has_delete;
    private List<People> staffs = new ArrayList<>();

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent);
    }

    class ViewHolder extends BaseViewHolder<People> {
        @BindView(R.id.checkbox)
        CheckBox checkbox;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.delete)
        ImageButton delete;
        @BindView(R.id.header)
        ImageView header;

        public ViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_people);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(final People data) {
            String dept = TextUtils.isEmpty(data.getDeptAndPos()) ? data.getDeptAndPosStr() : data.getDeptAndPos();
            name.setText(data.getEmplName() + " - " + dept);
            checkbox.setChecked(staffs.contains(data));

            if (has_delete) {
                delete.setVisibility(View.VISIBLE);
                checkbox.setVisibility(View.GONE);
                header.setVisibility(View.VISIBLE);
            } else {
                delete.setVisibility(View.GONE);
                checkbox.setVisibility(View.VISIBLE);
                header.setVisibility(View.GONE);
            }
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onDelectPeople != null) {
                        if (send != null) {
                            send.removeChoosePeople(data);
                        }
                        onDelectPeople.onDelete();
                    }
                }
            });
        }
    }

    public List<People> getStaffs() {
        return staffs;
    }

    public void setStaffs(List<People> staffs) {
        this.staffs = staffs;
    }

    public boolean isIs_single() {
        return is_single;
    }

    public void setIs_single(boolean is_single) {
        this.is_single = is_single;
    }

    public boolean isHas_delete() {
        return has_delete;
    }

    public void setHas_delete(boolean has_delete) {
        this.has_delete = has_delete;
    }

    public interface OnDelectPeople {
        void onDelete();
    }
}
